﻿using System;
using System.Linq;
using System.Text;
using WebService.Nafania.BussinesLayer.Model.Generator;

namespace UI.Nafania.Helpers
{
	public static class SnippetExtentions
	{
		public static string ToLineArray(this LineResult lr)
		{
			Guid[] ar = new Guid[lr.LinesCount];

			BlockLine[] data = lr.Blocks;

			
			StringBuilder sb = new StringBuilder();
			
			for (int i = 0; i < ar.Length; i++)
			{
				var currentLineNum = i + 1;

				var min = data.Where(x => x.Begin <= currentLineNum && x.End >= currentLineNum).Min(x => (currentLineNum - x.Begin));
				BlockLine blocks = data.Where(x => x.Begin <= currentLineNum && x.End >= currentLineNum).Where(x => (currentLineNum - x.Begin) == min)
						.Single();

				ar[i] = blocks.BlockId;

				sb.AppendFormat("{0}: '{1}',", currentLineNum, blocks.BlockId);
			}


			return "{" + sb.ToString().TrimEnd(',') + "}";

/*
			string result = string.Empty;

			foreach (BlockLine line in data)
				result = result + string.Format("{0}-{1},", line.Begin, line.End);

			return result.TrimEnd(',');
*/
		}

		public static string ToBlockArray(this LineResult lr)
		{
//			Guid[] ar = new Guid[lr.LineNumber];

			BlockLine[] data = lr.Blocks;

			
			StringBuilder sb = new StringBuilder();
			
/*			for (int i = 0; i < ar.Length; i++)
			{
				var currentLineNum = i + 1;

				var min = data.Where(x => x.Begin <= currentLineNum && x.End >= currentLineNum).Min(x => (currentLineNum - x.Begin));
				BlockLine blocks = data.Where(x => x.Begin <= currentLineNum && x.End >= currentLineNum).Where(x => (currentLineNum - x.Begin) == min)
						.Single();

				ar[i] = blocks.BlockId;

				sb.AppendFormat("{0}: '{1}',", currentLineNum, blocks.BlockId);
			}*/




			foreach (BlockLine line in data)
				sb.AppendFormat("'{0}': '{1}-{2}',", line.BlockId, line.Begin, line.End);
			//result = result + string.Format("{0}-{1},", line.Begin, line.End);

			return "{" + sb.ToString().TrimEnd(',') + "}";
		}
	}
}